layui.define(['jquery','upload'],function(exports){
	var $ = layui.$
	upload = layui.upload
	,obj = {
    	uploadSingleInit: function(objstring){
			upload.render({
				elem: '#'+objstring
				,url: MyUrl.uploadSingle
				,accept:'file'
				,before: function(obj){
					$('#'+objstring+'-list-div').html('<li><div class="layui-list-waiting" id="'+objstring+'-list-waiting"></div><a href="javascript:;" target="_blank"><img class="layui-list-img" id="'+objstring+'-list-img" /></a><p class="layui-list-text-single" id="'+objstring+'-list-text-single"></p></li>');
					obj.preview(function(index, file, result){
						$('#'+objstring+'-list-img').attr('src', result);
						$('#'+objstring+'-list-text-single').html('正在上传');
					});
				}
				,done: function(res){
					if(res.err){
						layer.msg(res.err);
					}else{
						$('#'+objstring+'-list-waiting').remove();
						$('#'+objstring+'-list-img').attr('src', res.msg)
						$('#'+objstring+'-list-img').parent('a').attr('href', res.msg)
						$('#'+objstring+'-list-text-single').html('<span style="color: green; text-algin:center">上传成功</span>');
						$('#'+objstring+'-list-div li').append('<input type="hidden" name="'+objstring+'" value="'+ res.msg +'" />');
						$('#'+objstring+'-list-div li').append('<span class="closed"></span>');
					}	
				}
				,error: function(){
					$('#'+objstring+'-list-waiting').remove();
					$('#'+objstring+'-list-text-single').html('<span style="color: red;">上传失败</span>');
					$('#'+objstring+'-list-div li').append('<span class="closed"></span>');
	
				}
			});	
		}
		,uploadMutliInit : function(objstring){
			upload.render({
				elem: '#'+objstring
				,url: MyUrl.uploadMutli
				,multiple: true
				,before: function(obj){
					obj.preview(function(index, file, result){
						if($('#'+objstring+'-list-li-'+index).length>0){
							$('#'+objstring+'-list-img-'+index).attr('src', result);
							$('#'+objstring+'-list-text-'+index).html('等待上传');
						}else{
							$('#'+objstring+'-list-div').append('<li id="'+objstring+'-list-li-'+index+'"><div class="layui-list-waiting" id="'+objstring+'-list-waiting-'+index+'"></div><a href="javascript:;" target="_blank"><img class="layui-list-img" id="'+objstring+'-list-img-'+index+'" /></a><p class="layui-list-text" id="'+objstring+'-list-text-'+index+'"></p></li>');
						};
						$('#'+objstring+'-list-img-'+index).attr('src', result);
						$('#'+objstring+'-list-text-'+index).html('等待上传');
					});
				}
				,done: function(res,index){
					if(res.err){
						layer.msg(res.err);
						$('#'+objstring+'-list-waiting-'+index).remove();
						$('#'+objstring+'-list-text-'+index).html('<span style="color: red;">上传失败</span>');
						$('#'+objstring+'-list-li-'+index).append('<span class="closed"></span>');
					}else{
						$('#'+objstring+'-list-waiting-'+index).remove();
						$('#'+objstring+'-list-img-'+index).attr('src', res.msg)
						$('#'+objstring+'-list-img-'+index).parent('a').attr('href', res.msg)
						$('#'+objstring+'-list-text-'+index).html('<span style="color: green; text-algin:center">上传成功</span>');
						$('#'+objstring+'-list-li-'+index).append('<input type="hidden" name="multiUrl[]" value="'+ res.msg +'" />');
						$('#'+objstring+'-list-li-'+index).append('<input type="text" class="multiInputTitle" name="multiTitle[]" />');
                		$('#'+objstring+'-list-li-'+index).append('<input type="text" class="multiInputOrder" name="multiOrder[]" />');
						$('#'+objstring+'-list-li-'+index).append('<input type="hidden" name="multiDefault[]" value="0">');
						$('#'+objstring+'-list-li-'+index).append('<span class="closed"></span>');

					}	
				}
				,allDone: function(obj){
					layer.msg('共上传 '+obj.total+' 个文件。<br />成功上传：'+obj.successful+'个。<br />上传失败：'+obj.aborted+'个。');
				}
				,error: function(index,upload){
					$('#'+objstring+'-list-waiting-'+index).remove();
					$('#'+objstring+'-list-text-'+index).html('<span style="color: red;">上传失败</span>');
					$('#'+objstring+'-list-li-'+index).append('<span class="closed"></span>');

				}
			})
		}
		
		
		
	};
	exports('uploads', obj);
});    

//js一般函数库